package auto.common;

import java.util.Random;

public class RandomStringGenerate {
    public static String generateRandomString(int length) {
        // 定义包含所有可能字符的字符集
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
        // 定义包含所有字母的字符集
        String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

        // 创建随机数生成器
        Random random = new Random();

        // 创建字符数组来存储随机字符
        char[] randomChars = new char[length];

        // 确保第一个字符是字母
        randomChars[0] = letters.charAt(random.nextInt(letters.length()));
        // 生成随机字符并填充到字符数组中
        for (int i = 1; i < length; i++) {
            randomChars[i] = characters.charAt(random.nextInt(characters.length()));
        }
        // 将字符数组转换为字符串并返回
        return new String(randomChars);
    }
}